package parkshop.pmcourse.controller;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import parkshop.pmcourse.domain.MallAdministrator;
import parkshop.pmcourse.service.MallAdministratorService;
import parkshop.pmcourse.utils.MD5Util;

@Controller
@RequestMapping("mallAdministrator/")
public class MallAdministratorController {
	
	private static final Logger logger = LoggerFactory.getLogger(MallAdministratorController.class);
	@Autowired
	private MallAdministratorService mallAdministratorService;
	
	@RequestMapping(value="toLogin", method=RequestMethod.GET)
	public String toLogin() {
		return "index";
	}
	
	@RequestMapping(value="login", method=RequestMethod.POST)
	public String login(String name, String password, Model model) {
		MallAdministrator mallAdministrator = mallAdministratorService.selectByName(name);
		if (mallAdministrator == null) {
			model.addAttribute("msg", "用户名不存在");
			logger.error("用户名不存在");
			return "mallAdministrator/loginUI";
		}
		
		if (StringUtils.equals(password, MD5Util.MD5(mallAdministrator.getPassword()))) {
			return "index";
		} else {
			model.addAttribute("msg", "密码错误");
			logger.error("密码错误");
			return "mallAdministrator/loginUI";
		}
	}
}
